package com.lzy.cloud.user.facade;

import com.lzy.cloud.user.config.SysUserAdminConfig;
import com.lzy.cloud.user.constant.RedisPrefix;
import com.lzy.cloud.user.dtos.SysUserAdminDto;
import com.lzy.cloud.user.remote.ShopApi;
import com.lzy.cloud.user.remote.dtos.ShopDto;
import com.lzy.cloud.user.request.LoginRequest;
import com.lzy.cloud.user.response.UserToken;
import com.lzy.cloud.user.service.SysUserAdminService;
import com.lzy.framework.core.cache.cluster.RedisClusterService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.core.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @Author: 19516
 * @Despriction:
 * @Date: 2018-06-04 16:47
 */
@Component
public class SysUserAdminFacade {

    @Autowired
    private SysUserAdminService sysUserAdminService;

    @Autowired
    private SysUserAdminConfig sysUserAdminConfig;

    @Autowired
    private RedisClusterService redisClusterService;

    @Autowired
    private ShopApi shopApi;

    /**
     * 登陆
     *
     * @param loginRequest
     * @return
     */
    public String login(LoginRequest loginRequest) {
        SysUserAdminDto sysUserAdminDto = sysUserAdminService.findByLoginName(loginRequest.getLoginName());
        Ensure.that(sysUserAdminDto).isNotNull("0000003");
        Ensure.that(Integer.valueOf(1).equals(sysUserAdminDto.getState())).isTrue("0000004");
        Ensure.that(MD5Utils.MD5(loginRequest.getPassword() + sysUserAdminConfig.getSalt()).equals(sysUserAdminDto.getPassword())).isTrue("0000005");
        String token = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        redisClusterService.setObject(RedisPrefix.TOKEN + token, DozerUtils.map(sysUserAdminDto, UserToken.class));
        redisClusterService.expire(RedisPrefix.TOKEN + token, sysUserAdminConfig.getTokenTimeout() * 60);
        return token;
    }

    /**
     * 密码修改，已知账号密码的情况下
     *
     * @param userId
     * @param originalPassword
     * @param newPassword
     * @return
     */
    public Boolean changePassword(Long userId, String originalPassword, String newPassword) {
        SysUserAdminDto sysUserAdminDto = sysUserAdminService.findById(userId);
        if (sysUserAdminDto != null) {
            String password = sysUserAdminDto.getPassword();
            String salt = sysUserAdminDto.getSalt();
            Ensure.that(MD5Utils.MD5(originalPassword + salt).equals(password)).isTrue("0000005");
            sysUserAdminDto.setPassword(MD5Utils.MD5(newPassword + salt));
            return sysUserAdminService.update(sysUserAdminDto) > 0;
        }
        return false;
    }

    /**
     * 新增商户
     * @param dto
     * @return
     */
    public Boolean addUser(SysUserAdminDto dto){
        String password = MD5Utils.MD5(dto.getPassword() + sysUserAdminConfig.getSalt());
        dto.setPassword(password);
        //新增商户
        ShopDto shopDto = new ShopDto();
        shopDto.setModuleStr(dto.getModuleStr());
        shopDto.setBodyService(dto.getBodyService());
        shopDto.setCantactPhone(dto.getPhone());
        Result result = shopApi.addShop(shopDto);
        Long shopId = Long.valueOf(result.getResult().toString());
        //新增用户
        dto.setShopId(shopId);
        Long id = sysUserAdminService.insert(dto);
        Ensure.that(id).isNotNull("");
        return true;
    }

}
